﻿Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class FrontEnd_Control_DangKyKDList
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindToGrid()
            'btnDelete.Attributes.Add("onclick", "return confirmMultiDelete('" & btnDelete.ClientID & "');")
        End If
    End Sub
    Private Sub BindToGrid(Optional ByVal iPage As Integer = 1, Optional ByVal strSearch As String = "")
        Using data As New VSEntities
            Dim _UserName As String = Session("UserName")
            'So ban ghi muon the hien tren trang
            Dim intPag_Size As Int32 = drpPage_Size.SelectedValue
            Dim p = (From q In data.CertifiedHouseholds Where q.Creator = _UserName
                     Order By q.Created Descending Select q).Skip((iPage - 1) * intPag_Size).Take(intPag_Size).ToList
            Dim strKey_Name() As String = {"CertifiedID", "Biz_VietName", "NganhNgheKinhDoanh", "LinhVucKinhDoanh", "StatusId", "CertifiedUp_Time"}
            'Tong so ban ghi
            hidCount.Value = (From q In data.CertifiedHouseholds Where q.Creator = _UserName
                              Order By q.Created Descending Select q).Count
            Create_Pager(hidCount.Value, iPage, intPag_Size, 10)
            With grdShow
                .DataKeyNames = strKey_Name
                .DataSource = p
                .DataBind()
            End With
            lblTotal.Text = "Hiển thị " + (((iPage - 1) * intPag_Size) + 1).ToString + " đến " + (((iPage - 1) * intPag_Size) + grdShow.Rows.Count).ToString + " trong tổng số " + hidCount.Value.ToString + " bản ghi."
        End Using
    End Sub
    Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub
    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)
        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.Text = "<span class='current'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub
    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        BindToGrid(lnkTile.ToolTip)
        lnkLast.Enabled = True
        lnkFirst.Enabled = True
        If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf CInt(lnkTile.ToolTip) = 1 Then
            lnkFirst.Enabled = False
        End If
    End Sub
    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click
        If hidCur_Page.Value > 1 Then
            hidCur_Page.Value = hidCur_Page.Value - 1
        End If
        BindToGrid(hidCur_Page.Value)
    End Sub
    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        BindToGrid(hidCur_Page.Value)
    End Sub
#End Region
#Region "Event for control"
    Protected Sub lnkbtnDelete_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim intId As Integer
        Dim strLogName As String = ""
        Using data As New VSEntities
            intId = grdShow.DataKeys(hidID.Value)("CertifiedID").ToString
            Dim q = (From p In data.CertifiedHouseholds Where p.CertifiedID = intId Select p).FirstOrDefault
            Try
                data.CertifiedHouseholds.DeleteObject(q)
                data.SaveChanges()
                Insert_App_Log("Insert  CertifiedHousehold:" & q.CertifiedCode & "", Function_Name.CertifiedHousehold, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Xóa dữ liệu thành công.');", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Xóa thất bại.');", Me.Page, True)
            End Try
        End Using
        BindToGrid()
    End Sub

    Protected Sub grdShow_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShow.RowDataBound
        Using data As New VSEntities
            If e.Row.RowIndex >= 0 Then
                'load nganh nghe kinh doanh theo bản Danhmucnganhnghes by Id
                Dim lblNganhNgheKinhDoanh As Label = CType(e.Row.FindControl("lblNganhNgheKinhDoanh"), Label)
                Dim IdNganhNghe As Integer = grdShow.DataKeys(e.Row.RowIndex)("NganhNgheKinhDoanh")
                'load ngành nghề kinh doanh
                Dim IdCertified As Integer = grdShow.DataKeys(e.Row.RowIndex)("CertifiedID")
                Dim NganhNghe = (From q In data.CertifiedHouseholdNganhNghes Where q.CertifiedID = IdCertified Order By q.Id Select q.DanhMucNganhNgheID).ToArray
                Dim TenNganhNghe = (From q In data.DanhMucNganhNghes Where NganhNghe.Contains(q.Id) Order By q.Name Select q.Name).ToList
                Dim ltrNganhNgheKinhDoanh As Literal = CType(e.Row.FindControl("ltrNganhNgheKinhDoanh"), Literal)
                Dim listNganhNghe As String = ""
                For Each item In TenNganhNghe
                    listNganhNghe += " - " & item & "<br/>"
                Next
                ltrNganhNgheKinhDoanh.Text = listNganhNghe

                'load linh vực kinh doanh theo bảng BusinessFields By Id
                Dim lblLinhVucKinhDoanh As Label = CType(e.Row.FindControl("lblLinhVucKinhDoanh"), Label)
                Dim Idlinhvuc As Integer = grdShow.DataKeys(e.Row.RowIndex)("LinhVucKinhDoanh").ToString
                Dim linhvuc = (From q In data.BusinessFields Where q.Id = Idlinhvuc Select q.Id, q.Name).FirstOrDefault
                lblLinhVucKinhDoanh.Text = linhvuc.Name
                'tinh trang ho so

                Dim lblStatusId As Label = CType(e.Row.FindControl("lblStatusId"), Label)
                Dim _l As Integer = grdShow.DataKeys(e.Row.RowIndex)("StatusId")
                lblStatusId.Text = (From h In data.Status Where h.StatusId = _l Select h.TitleHoSo).FirstOrDefault


                Dim lblSTT As Label = CType(e.Row.FindControl("lblSTT"), Label)
                lblSTT.Text = CInt(drpPage_Size.SelectedValue) * (CInt(hidCur_Page.Value) - 1).ToString + e.Row.RowIndex + 1
                Dim hplTitle As HyperLink = CType(e.Row.FindControl("hplTitle"), HyperLink)
                hplTitle.NavigateUrl = MainDomain & "xem-giay-de-nghi-dang-ky.html?Id=" & grdShow.DataKeys(e.Row.RowIndex)("CertifiedID").ToString
                hplTitle.Text = grdShow.DataKeys(e.Row.RowIndex)("Biz_VietName").ToString

                Dim hplEdit As HyperLink = CType(e.Row.FindControl("hplEdit"), HyperLink)
                hplEdit.NavigateUrl = MainDomain & "sua-giay-de-nghi-dang-ky.html?Id=" & grdShow.DataKeys(e.Row.RowIndex)("CertifiedID").ToString
                'trang thai ho so lblCertifiedUp_Time
                Dim lblCertifiedUp_Time As Label = CType(e.Row.FindControl("lblCertifiedUp_Time"), Label)
                lblCertifiedUp_Time.Text = IIf(grdShow.DataKeys(e.Row.RowIndex)("CertifiedUp_Time") = 0, "Cấp mới", "Cấp đổi")

            End If
        End Using
    End Sub
#End Region
#Region "Search"
    Protected Sub btnFilter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFilter.Click
        BindToGrid(1, txtTitleFilter.Text.Trim())
    End Sub
#End Region
End Class
